package com.zzptc.sky.translate;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity {

    ViewPager viewPager;
    List<View> list;
    Button button;
    TextView textView;
    LinearLayout linearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        viewPager = (ViewPager) findViewById(R.id.viewpager_guide);
        textView = (TextView) findViewById(R.id.tv_guide);
        linearLayout= (LinearLayout) findViewById(R.id.linearLayout_guide);
        initData();
    }

    public void initData() {
            View point = null;
            for (int i = 0; i < 5; i++) {
                point = new View(this);
                point.setEnabled(false);
                //设置圆圈的样子   选择器来实现
                point.setBackgroundResource(R.drawable.point_select);
                //设置大小
                LinearLayout.LayoutParams params = new LinearLayout
                        .LayoutParams(20, 20);
                //点跟点之间的距离
                params.leftMargin = 20;
                //将自定义的控件加入布局文件中
                linearLayout.addView(point, params);
            }
            //选择第一个点  并设置它的属性Enabled  为true
            linearLayout.getChildAt(0).setEnabled(true);

        // 准备显示的图片集合
        list=new ArrayList<View>();
        View view1 = View.inflate(this, R.layout.activity_guide_one, null);
        View view2 = View.inflate(this, R.layout.activity_guide_two, null);
        View view3 = View.inflate(this, R.layout.activity_guide_three, null);
        View view4 = View.inflate(this, R.layout.activity_guide_four, null);
        View view5 = View.inflate(this, R.layout.activity_guide_go, null);

        list.add(view1);
        list.add(view2);
        list.add(view3);
        list.add(view4);
        list.add(view5);
        MyAdapter myAdapter=new MyAdapter(list);
        viewPager.setAdapter(myAdapter);

        button= (Button) list.get(4).findViewById(R.id.button_go);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Util.set(getApplicationContext());
                //跳转
                System.out.println("=============");
                startActivity(new Intent(GuideActivity.this,MainActivity.class));
                finish();
            }
        });
        //点击跳过文本框时   跳到最后一个滑动页面上
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(list.size()-1);
            }
        });
        //设置滑动页面的监听
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                for(int i=0;i<list.size();i++){
                    if (i==position){
                        linearLayout.getChildAt(i).setEnabled(true);
                    }else {
                        linearLayout.getChildAt(i).setEnabled(false);
                    }
                }
                //控制“跳过”文本框   在最后滑动页面中让文本隐藏
                switch (position){
                    case 0:
                        textView.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        textView.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        textView.setVisibility(View.VISIBLE);
                        break;
                    case 3:
                        textView.setVisibility(View.VISIBLE);
                        break;
                    case 5:
                        textView.setVisibility(View.VISIBLE);
                        break;
                    case 4:
                        textView.setVisibility(View.INVISIBLE);
                        break;
                }
            }


            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
